package com.jeejam.product.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jeejam.common.util.TimeUtil;
import com.jeejam.framework.base.service.impl.BaseServiceImpl;
//import com.jeejam.member.pojo.Vote;
//import com.jeejam.member.service.CommentService;
//import com.jeejam.member.service.VoteService;
import com.jeejam.product.dao.ProductMapper;
import com.jeejam.product.pojo.Product;
import com.jeejam.product.service.ProductService;

@Service
public class ProductServiceImpl extends BaseServiceImpl implements ProductService{
	
	private ProductMapper productMapper;
	
//	private CommentService commentService;
//
//	private VoteService voteService;

	@Resource
	public void setProductMapper(ProductMapper productMapper) {
		this.productMapper = productMapper;
		this.baseMapper = productMapper;
	}

	@Override
	public List<Product> findByBrandId(int brandId, int page, int pageSize){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("brandId", brandId);
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productMapper.selectByBrandId(params);
	}

	@Override
	public List<Product> findByBrandName(String brandName, int page,int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("brandName", brandName);
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productMapper.selectByBrandName(params);
	}

	@Override
	public Product getByCode(String productCode) {
		return productMapper.selectByCode(productCode);
	}

	@Override
	public List<Product> findForRecommend(int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productMapper.selectForRecommendByPage(params);
	}

	@Override
	public List<Product> findForNew(int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productMapper.selectForNewByPage(params);
	}

	@Override
	public List<Product> findForBest(int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productMapper.selectForBestByPage(params);
	}

	@Override
	public List<Product> findForHot(int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productMapper.selectForHotByPage(params);
	}

	@Override
	public int countsForRecommend() {
		return productMapper.countsForRecommend();
	}

	@Override
	public int countsForNew() {
		return productMapper.countsForNew();
	}

	@Override
	public int countsForBest() {
		return productMapper.countsForBest();
	}

	@Override
	public int countsForHot() {
		return productMapper.countsForHot();
	}

	@Override
	public int getVotes(int id) {
		return productMapper.selectVotes(id);
	}

	@Override
	public void updateViews(int id) {
		productMapper.updateViews(id);
	}

	@Override
	public void updateVotes(int votes, int id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("votes", votes);
		params.put("id", id);
		productMapper.updateVotes(params);
	}

	@Override
	public List<Map<String, Object>> createExcelRecord(List<Product> products) {
		List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sheetName", "sheet1");
        listmap.add(map);
        Product product = null;
        for (int j = 0; j < products.size(); j++) {
        	product = products.get(j);
            Map<String, Object> mapValue = new HashMap<String, Object>();
            mapValue.put("id", product.getId());
            mapValue.put("code", product.getCode());
            mapValue.put("name", product.getName());
            if(product.getCategory() != null){
            	mapValue.put("category", product.getCategory().getName());
            }else{
            	mapValue.put("category", "");
            }
           
            mapValue.put("spec", product.getSpec());
            mapValue.put("color", product.getColor());
            mapValue.put("brief", product.getBrief());
            mapValue.put("content", product.getContent());
            if(product.getBrand() != null){
            	mapValue.put("brand", product.getBrand().getBrandName());
            }else{
            	mapValue.put("brand", "");
            }
            mapValue.put("marketPrice", product.getMarketPrice());
            mapValue.put("shopPrice", product.getShopPrice());
            mapValue.put("keywords", product.getKeywords());
            mapValue.put("description", product.getDescription());
            mapValue.put("thumb", product.getThumb());
            if(product.getIsReal() == 1){
            	mapValue.put("isReal", "实物");
            }else{
            	mapValue.put("isReal", "虚拟");
            }
            if(product.getIsOnSale() == 1){
            	mapValue.put("isOnSale", "上架");
            }else{
            	mapValue.put("isOnSale", "下架");
            }
            if(product.getIsAloneSale() == 1){
            	mapValue.put("isAloneSale", "单独销售");
            }else{
            	mapValue.put("isAloneSale", "绑定销售");
            }
            if(product.getIsShipping() == 1){
            	mapValue.put("isShipping", "免运费");
            }else{
            	mapValue.put("isShipping", "需要运费");
            }
            mapValue.put("create_time", TimeUtil.toString(product.getCreateTime(),null));
            listmap.add(mapValue);
        }
        return listmap;
	}

	@Override
	public List<Product> findByCategoryId(int rootCategoryId, int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryId", rootCategoryId);
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productMapper.selectByCategoryId(params);
	}

	@Override
	public List<Map<String, Object>> show(int userId, List<Product> products) {
		List<Map<String, Object>> productList = new ArrayList<Map<String, Object>>();
		for(Product product : products){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("product", product);
//	        map.put("comments", commentService.countsByProductId(product.getId()));
	        if(userId != 0){
//				Vote vote = voteService.selectByProductIdAndUserId(product.getId(), userId);
//	        	if(vote != null){
//	        		map.put("vote", vote.getValue());
//	        	}
			}
	        productList.add(map);
		}
		return productList;
	}

	@Override
	public int countsByCategoryId(int rootCategoryId) {
		return productMapper.countsByCategoryId(rootCategoryId);
	}

	@Override
	public List<Product> findForNewByCategoryId(int rootCategoryId, int page,
			int pageSize) {
		return this.findByCategoryId(rootCategoryId, page, pageSize);
	}

	@Override
	public int countsForNewByCategoryId(int rootCategoryId) {
		return this.countsByCategoryId(rootCategoryId);
	}

	@Override
	public List<Product> findForHotByCategoryId(int rootCategoryId, int page,
			int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("rootCategoryId", rootCategoryId);
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productMapper.selectForHotByCategoryId(params);
	}

	@Override
	public int countsForHotByCategoryId(int rootCategoryId) {
		return productMapper.countsForHotByCategoryId(rootCategoryId);
	}

	@Override
	public List<Product> findForRecommendByCategoryId(int rootCategoryId,
			int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("rootCategoryId", rootCategoryId);
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return productMapper.selectForRecommendByCategoryId(params);
	}

	@Override
	public int countsForRecommendByCategoryId(int rootCategoryId) {
		return productMapper.countsForRecommendByCategoryId(rootCategoryId);
	}

}
